import { Component, OnInit } from '@angular/core';
import {ServiceLogicService} from '../../service/service-logic.service';
import { Attendance, Leave, Out } from '../dataDomain';
import { ABSENCE } from '../data';

@Component({
  selector: 'app-absence-day-record',
  templateUrl: './absence-day-record.component.html',
  styleUrls: ['./absence-day-record.component.css']
})
export class AbsenceDayRecordComponent implements OnInit {

  admin_no:string;
  admin_id:string;
  start_time:string;
  month : string;

  flag = 0;
  attend:Attendance[];
  leave : Leave[];
  out : Out[];
  absence = ABSENCE;
  g=[];

  ou=[];
  le=[];

  constructor(private myService:ServiceLogicService) { }

  ngOnInit() {
    this.myService.getAllAttendanceRecords().then(res=>this.attend=res);
    this.myService.getAllEmployeeLeaveRecord().then(res=>this.leave=res);
    this.myService.getAllEmployeeOutRecords().then(res=>this.out=res);
  }

  sort(){
    this.flag=1;
    this.admin_no = (document.getElementById("type") as HTMLInputElement).value;
    this.admin_id = (document.getElementById("admin_id") as HTMLInputElement).value;
    this.start_time = (document.getElementById("start_time") as HTMLInputElement).value;
    this.g=[];
    this.ou=[];
    this.le=[];
    for(let n of this.absence){
      n.absenceDay=0;
    }

    if(this.admin_id==""){
     // this.flag=1;
      if(this.admin_no!="0"){
       // this.flag=2;
      for(let a of this.attend ){
        //let date = a.year+"/"+a.month;
        if(a.month=="10"||a.month=="11"||a.month=="12")
        {
           this.month= a.month;
        }
        else{
          this.month = "0"+a.month;
        }
        let date = a.year+"-"+this.month;
        if(this.start_time==date && this.admin_no==a.dept_no)
         {
           this.flag=3;
          this.g.push(a);
         }
        else
         continue;
       }
      }

     else{
       if(this.start_time==""){
         alert('请输入查询参数');
         window.location.reload();
       }
      for(let a of this.attend){
      //  this.flag=4;
       // let date = a.year+"/"+a.month;
       if(a.month=="10"||a.month=="11"||a.month=="12")
       {
          this.month= a.month;
       }
       else{
         this.month = "0"+a.month;
       }
       let date = a.year+"-"+this.month;
        if(this.start_time==date )
         {
        //   this.flag=3;
           this.g.push(a);
         }
         else{
         continue;
         }
       }
      }
    }

  else{
    for(let a of this.attend ){
     // let date = a.year+"/"+a.month;
     if(a.month=="10"||a.month=="11"||a.month=="12")
     {
        this.month= a.month;
     }
     else{
       this.month = "0"+a.month;
     }
     let date = a.year+"-"+this.month;
     if(this.start_time==date && this.admin_id==a.admin_id)
     {
      this.g.push(a);
     }
     else
     continue;
   }
  }

  for(let a of this.g){
    for(let m of this.absence)
    if(a.admin_id==m.admin_id){
      m.absenceDay=m.absenceDay+1;
    }
    else
    continue;
  }
  
  for(let a of this.leave){
   // this.flag=100;

   //let tp = a.satrt_time.split("/");
   //a.satrt_time = tp[0]+tp[1];
   //if(this.start_time==a.satrt_time)
   //a.satrt_time.replace("/","");
    for(let m of this.absence){
      if(a.admin_id==m.admin_id 
        && a.satrt_time.includes((this.start_time))
        &&(a.type==3||a.type==4)){
         m.absenceDay=m.absenceDay+(+a.days);
         this.le.push(a);
      }
      else
      continue;
    }
  }

  for(let a of this.out){

   //let tp = a.satrt_time.split("/");
   //a.satrt_time = tp[0]+tp[1];
   //if(this.start_time==a.satrt_time)
   // a.satrt_time.replace("/","");
    for(let m of this.absence){
      if(a.admin_id==m.admin_id
        &&a.satrt_time.includes((this.start_time))){
          m.absenceDay=m.absenceDay+(+a.days);
          this.ou.push(a);
        }
      else
      continue;
    }
  }
 }
}

